#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_deque)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/deque.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "deque.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    static deque_t deque = NULL; 
    static int initFlag = 0;
    static unsigned int start = 0x7FFFFFFF, end = 0x7FFFFFFF;

    unsigned int data = 0;
    unsigned int size = end - start;
    int op = rand() % 4;

    if (!initFlag)
    {
        deque = deque(unsigned int, 10);
        initFlag = 1;
    }

    if (size != deque_size(deque)) return UNITT_E_FAIL;
    if (size > 0)
    {
        data = rand() % deque_size(deque);
        if (deque_at(deque, unsigned int,data) != (start + data)) return UNITT_E_FAIL;
    }

    printf("size %u\r\n", size);
    
    switch (op)
    {
    case 0:
        if (deque_push_front(deque, ((unsigned int[1]){start - 1}))) start--;
        break;
    case 1:
        if (deque_push_back(deque, &end)) end++;
        break;
    case 2:
        if (deque_pop_front(deque, NULL)) start++;
        break;
    case 3:
        if (deque_pop_back(deque, NULL)) end--;
        break;
    default:
        break;
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "deque suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

static void test_create(void) 
{
    deque_t deque = deque(int, 10);
    
    if (deque)
    {
        printf("deque create success!!!\r\n");
    }
    else  
    {
        printf("[ERROR] deque create fail!!!\r\n");
    }

    _deque(deque);
}

static void test_push(void) 
{
    deque_t deque = deque(int, 10);
    int data = 0;
    
    if (!deque) 
    {
        printf("[ERROR] deque create fail!!!\r\n");
        return;
    }

    data = 1; deque_push_back(deque, &data);
    data = 2; deque_push_back(deque, &data);
    data = -100; deque_push_front(deque, &data);

    for (int i = 0; i < deque_size(deque); i++)
    {
        printf("deque[%d] = %d\r\n", i, deque_at(deque, int, i));
    }

    _deque(deque);
}

static void test_pop(void) 
{
    deque_t deque = deque(int, 10);
    
    if (!deque) 
    {
        printf("[ERROR] deque create fail!!!\r\n");
        return;
    }

    for (int i = 0; i < deque_capacity(deque); i++)
    {
        deque_push_back(deque, &i);
    }

    deque_pop_back(deque, NULL);
    deque_pop_back(deque, NULL);
    deque_pop_front(deque, NULL);

    for (int i = 0; i < deque_size(deque); i++)
    {
        printf("deque[%d] = %d\r\n", i, deque_at(deque, int, i));
    }

    _deque(deque);
}

static void test_base(void) 
{
    deque_t deque = deque(int, 10);
    int i = 0;

    for (i = 0; i < deque_capacity(deque); i++)
    {
        deque_push_back(deque, &i);
    }
    deque_pop_front(deque, NULL);
    deque_pop_back(deque, NULL);
    for (i = 0; i < deque_size(deque); i++)
    {
        printf("deque[%d] = %d\r\n", i, deque_at(deque, int, i));
    }

    _deque(deque);
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: deque [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"                        <create>    Test create and delete functions\n"
"                        <push>      Test push functions\n"
"                        <pop>       Test pop functions\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"
    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("deque version %d.%d.%d\r\n", DEQUE_V_MAJOR, DEQUE_V_MINOR, DEQUE_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            srand((unsigned int)time(NULL));
            #if defined(TEST_TARGET_deque)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
        else if (!strcmp(execute, "create"))
        {
            test_create();
        }
        else if (!strcmp(execute, "push"))
        {
            test_push();
        }
        else if (!strcmp(execute, "pop"))
        {
            test_pop();
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_deque)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_deque(void)
{
    command_export("deque", test);
}
init_export_app(test_deque);
#endif 
